package io.xxx.omni.osc.config

import okhttp3.OkHttpClient
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.cloud.client.loadbalancer.LoadBalanced
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory
import org.springframework.web.client.RestTemplate
import java.time.Duration

@Configuration
class WebConfig {

    @Bean
    fun restTemplate(builder: RestTemplateBuilder): RestTemplate {
        val httpClient = OkHttpClient
            .Builder()
            .readTimeout(Duration.ofSeconds(0))
            .callTimeout(Duration.ofSeconds(0))
            .writeTimeout(Duration.ofSeconds(0))
            .connectTimeout(Duration.ofSeconds(0))
            .build()

        return builder
            .requestFactory { OkHttp3ClientHttpRequestFactory(httpClient) }
            .build()
    }

    @Bean
    @LoadBalanced
    fun lbRestTemplate(): RestTemplate {
        return RestTemplate()
    }
}